package com.hmall.item.service.web;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.client.MyFeignClient;
import com.hmall.item.service.constants.MqConstants;
import com.hmall.search.common.dto.PageDTO;
import com.hmall.item.service.pojo.Item;
import com.hmall.item.service.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private IItemService itemService;

    @Autowired
    private MyFeignClient myFeignClient;

    @PostMapping
    public void add(@RequestBody Item item) {
        boolean b = itemService.save(item);
        System.out.println(b);
//        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_INSERT_KEY, item.getId());
//        log.info("成功发送save，id{}",item.getId());
    }

    @PutMapping
    public void update(@RequestBody Item item) {
        boolean b = itemService.updateById(item);
        System.out.println(b);
//        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_INSERT_KEY, item.getId());
//        log.info("成功发送update，id{}",item.getId());
    }

    @GetMapping("list")
    public PageDTO<Item> list(@RequestParam("page") Long page, @RequestParam("size") Long size) {
        IPage<Item> itemPage = new Page<>(page, size);
        IPage<Item> page1 = itemService.page(itemPage);
        long total = page1.getTotal();
        List<Item> list = page1.getRecords();
        return new PageDTO<Item>(total, list);
    }

    @GetMapping("{id}")
    public Item findById(@PathVariable("id") Long id) {
        Item byId = itemService.getById(id);
        return byId;
    }

    @PutMapping("status/{id}/{status}")
    public void updateStatus(@PathVariable("id") Long id, @PathVariable("status") Long status) {
        LambdaUpdateWrapper<Item> wrapper = Wrappers.<Item>lambdaUpdate();
        wrapper.eq(Item::getId, id).set(Item::getStatus, status);
        boolean b = itemService.update(wrapper);
        System.out.println(b);
//        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_QUEUE,status);
//        log.info("成功发送更新，status{}",status);
        if (status == 1) {
            rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_INSERT_KEY, id);
            log.info("成功发送更新上架，id{}", id);
        } else {
            rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_DELETE_KEY, id);
            log.info("成功发送更新下架，id{}", id);
        }
    }

    @DeleteMapping("{id}")
    public void deleteById(@PathVariable("id") Long id) {
        boolean b = itemService.removeById(id);
//        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_DELETE_KEY,id);
        System.out.println(b);
//        log.info("成功发送delete，id{}",id);
    }

    @PutMapping("stock/{itemId}/{num}")
    void updateItemStock(@PathVariable("itemId") Long itemId, @PathVariable("num") Integer num) {
        Item item = itemService.getById(itemId);
        item.setStock(item.getStock() - num);
        itemService.updateById(item);
    }
}
